package com.jacken.mars.commons.mq.base.producer;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;

/**
 * @author wangqiang
 * @version 1.0
 * @date 2021/1/2 17:58
 * 发送异步消息
 */
public class AsyncProducer {

    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("async-group-01");
        producer.setNamesrvAddr("114.55.146.111:9876");
        producer.start();

        for (int i = 0; i < 10; i++) {
            Message message = new Message("Async-Producer-Topic", "Tag-02", "hello world".getBytes());
            producer.send(message, new SendCallback() {
                @Override
                public void onSuccess(SendResult result) {
                    System.out.println("sendResult" + result);
                    SendStatus sendStatus = result.getSendStatus();
                    String msgId = result.getMsgId();
                    MessageQueue messageQueue = result.getMessageQueue();
                    System.out.println(result);
                    System.out.println("发送状态:" + sendStatus + "消息ID" + msgId + "消息队列" + messageQueue);
                }

                @Override
                public void onException(Throwable throwable) {
                    System.out.println("发送异常" + throwable);
                }
            });

            Thread.sleep(1000);

        }


        producer.shutdown();

    }
}
